package dnl.util.ui.table;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;

/**
  * 
  * 
  * @author Daniel Or
  * @version 1.0
  */
public class CellRenderer extends DefaultTableCellRenderer {

	/**
	 * Constructor for CellRenderer.
	 */
	public CellRenderer() {

	}

	@Override
	public Component getTableCellRendererComponent(
		JTable table,
		Object value,
		boolean isSelected,
		boolean hasFocus,
		int row,
		int column) {

		if (!(table instanceof CustomizedTable))
			throw new IllegalArgumentException(
				"This CellRenderer can be used only with CustomizedTable");
				
		CustomizedTable dtable = (CustomizedTable)table;
		TModel tmodel = (TModel)table.getModel();
		RowData rw = tmodel.getRow(row);
		
		this.setIcon(rw.getIcon(column));
		
//		Color frgrnd = rw==null ? null : rw.getForeground(column);
//		Color bckgrnd = rw==null ? null : rw.getBackground(column);
		
		if (isSelected) {
			super.setForeground(table.getSelectionForeground());
			super.setBackground(table.getSelectionBackground());
		} else {
			super.setForeground(table.getForeground());
			super.setBackground(table.getBackground());
		}

		setFont(table.getFont());

		if (hasFocus && dtable.isSelectionBorderShown()) {
			setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
			if (table.isCellEditable(row, column)) {
				super.setForeground(
					UIManager.getColor("Table.focusCellForeground"));
				super.setBackground(
					UIManager.getColor("Table.focusCellBackground"));
			}
		} else {
			setBorder(noFocusBorder);
		}

		setValue(value);

//		if( bckgrnd != null ){
//			super.setBackground(Color.red);
//		}
//		
//		if( frgrnd != null )
//			super.setForeground(frgrnd);
		
		return this;
	}

	/**
	 * Overrides <code>JComponent.setForeground</code> to assign
	 * the unselected-foreground color to the specified color.
	 * 
	 * @param c set the foreground color to this value
	 */
	@Override
	public void setForeground(Color c) {
		super.setForeground(c);
	}

	/**
	 * Overrides <code>JComponent.setForeground</code> to assign
	 * the unselected-background color to the specified color.
	 *
	 * @param c set the background color to this value
	 */
	@Override
	public void setBackground(Color c) {
		super.setBackground(c);
	}

}
